package com.streamer.app.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.streamer.app.R
import com.streamer.app.model.User
import com.streamer.app.model.UserRole
import com.streamer.app.utils.DialogUtils
import kotlin.random.Random

class UserListAdapter(
    private val users: MutableList<User>,
    private val onUserAction: (User, String) -> Unit,
    private val isMutedList: Boolean = false,
    private val isType:String="禁言名单"

) : RecyclerView.Adapter<UserListAdapter.UserViewHolder>() {

    class UserViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val ivUserAvatar: ImageView = itemView.findViewById(R.id.iv_user_avatar)
        val tvUserName: TextView = itemView.findViewById(R.id.tv_user_name)
        val tvUserRole: TextView = itemView.findViewById(R.id.tv_user_role)
        val llActionButtons: LinearLayout = itemView.findViewById(R.id.ll_action_buttons)
        val btnAction1: TextView = itemView.findViewById(R.id.btn_action_1)
        val btnAction2: TextView = itemView.findViewById(R.id.btn_action_2)
        val btnAction3: TextView = itemView.findViewById(R.id.btn_action_3)
    }

    // 随机头像资源数组
    private val randomAvatarResources = arrayOf(
        R.mipmap.user1, R.mipmap.user2, R.mipmap.user3, R.mipmap.user4, R.mipmap.user5,
        R.mipmap.user6, R.mipmap.user7, R.mipmap.user8, R.mipmap.user9, R.mipmap.user10,
        R.mipmap.user11, R.mipmap.user12, R.mipmap.user13, R.mipmap.user14, R.mipmap.user15,
        R.mipmap.user16, R.mipmap.user17, R.mipmap.user18, R.mipmap.user19, R.mipmap.user20
    )

    // 获取随机头像资源
    private fun getRandomAvatarResource(): Int {
        return randomAvatarResources[Random.nextInt(randomAvatarResources.size)]
    }

    // 根据用户ID获取固定的随机头像资源
    private fun getFixedRandomAvatarResource(userId: String): Int {
        val index = userId.hashCode().let { if (it < 0) -it else it } % randomAvatarResources.size
        return randomAvatarResources[index]
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): UserViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_user, parent, false)
        return UserViewHolder(view)
    }

    override fun onBindViewHolder(holder: UserViewHolder, position: Int) {
        val user = users[position]
        
        // 设置用户信息
        holder.tvUserName.text = user.name
        
        // 为每个用户生成一个固定的随机头像（基于用户ID或位置）
        val randomAvatar = getFixedRandomAvatarResource(user.id)
        
        holder.ivUserAvatar.apply {
            Glide.with(this).load(user.avatar)
                .error(randomAvatar)
                .placeholder(randomAvatar)
                .transform(RoundedCorners(50))
                .into(this)
        }
        // 根据角色设置标签
        when (user.role) {
            UserRole.STREAMER -> {
                holder.tvUserRole.text = "主播"
                holder.tvUserRole.setBackgroundResource(R.drawable.blue_primary)
                holder.tvUserRole.visibility = View.VISIBLE
                holder.llActionButtons.visibility = View.GONE
            }
            UserRole.ASSISTANT -> {
                holder.tvUserRole.text = "助理"
                holder.tvUserRole.setBackgroundResource(R.drawable.orange)
                holder.tvUserRole.visibility = View.VISIBLE
                holder.llActionButtons.visibility = View.GONE
            }
            UserRole.VIEWER -> {
                holder.tvUserRole.visibility = View.GONE
                holder.llActionButtons.visibility = View.VISIBLE
                
                // 根据列表类型设置按钮
                if (isMutedList) {
                    // 禁言名单：只显示解除禁言按钮
                    if (isType.contains("禁言")) {
                        holder.btnAction1.text = "解除禁言"

                    }else{
                        holder.btnAction1.text = "解除踢出"

                    }
                    holder.btnAction2.visibility = View.GONE
                    holder.btnAction3.visibility = View.GONE
                    
                    holder.btnAction1.setOnClickListener {
                        DialogUtils.showUnmuteConfirmDialog(holder.itemView.context, user.name) {
                            onUserAction(user, "unmute")
                        }
                    }
                } else {
                    // 普通用户列表：显示完整操作按钮
                    if (user.isMuted) {
                        holder.btnAction1.text = "解除禁言"
                    } else {
                        holder.btnAction1.text = "禁言"
                    }
                    
                    holder.btnAction2.text = "踢出房间"
                    holder.btnAction3.text = "加入黑名单"
                    
                    holder.btnAction2.visibility = View.VISIBLE
                    holder.btnAction3.visibility = View.VISIBLE
                    
                    // 设置按钮点击事件
                    holder.btnAction1.setOnClickListener {
                        if (user.isMuted) {
                            DialogUtils.showUnmuteConfirmDialog(holder.itemView.context, user.name) {
                                onUserAction(user, "unmute")
                            }
                        } else {
                            DialogUtils.showMuteConfirmDialog(holder.itemView.context, user.name) {
                                onUserAction(user, "mute")
                            }
                        }
                    }
                    
                    holder.btnAction2.setOnClickListener {
                        DialogUtils.showKickConfirmDialog(holder.itemView.context, user.name) {
                            onUserAction(user, "kick")
                        }
                    }
                    
                    holder.btnAction3.setOnClickListener {
                        DialogUtils.showBlacklistConfirmDialog(holder.itemView.context, user.name) {
                            onUserAction(user, "blacklist")
                        }
                    }
                }
            }
        }
    }

    override fun getItemCount(): Int = users.size
    
    fun updateUsers(newUsers: List<User>) {
        users.clear()
        users.addAll(newUsers)
        notifyDataSetChanged()
    }
    
    fun getUsers(): List<User> = users.toList()
} 